package cn.kbyue.redisdemo.service;

import cn.kbyue.redisdemo.config.RedisConfig;
import cn.kbyue.redisdemo.entity.Product;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * demo 直接使用实现类, 不再定义接口.
 * @author xlang
 * @date 2023/3/21 15:07
 */
@Service
public class ProductService {

    @CacheEvict(value = RedisConfig.REDIS_KEY_DATABASE, key = "'rs:product:'+#id")
    public int update(Long id, Product product) {
        System.out.println("[update] id=" + id + ", product: " + product);
        return 1;
    }

    @CacheEvict(value = RedisConfig.REDIS_KEY_DATABASE, key = "'rs:product:'+#id")
    public int delete(Long id) {
        System.out.println("[delete] id=" + id);
        return 1;
    }

    @Cacheable(value = RedisConfig.REDIS_KEY_DATABASE, key = "'rs:product:'+#id", unless = "#result==null")
    public Product getItem(Long id) {
        return new Product(id, "name-" + id);
    }

}
